package com.zz.box;

public class ZZQueue {
    private int[] array;
    private int front;
    private int rear;

    public ZZQueue(int capacity) {
        this.array = new int[capacity];
    }

    /**
     * 入队
     * @param element
     * @return
     */
    public boolean enQueue(int element){
        if((rear+1) % array.length == front){
            return false;
        }
        array[rear] = element;
        rear = (rear+1) % array.length;
        return true;
    }

    public int deQueue(){
        if(rear == front){
            throw new RuntimeException("队列已空！");
        }
        int deQueueElement = array[front];
        /**
         * 共6个
         * 0 1
         * 1 2
         * 5 0
         */
        front = (front+1) % array.length;
        return deQueueElement;
    }

    public void outputQueue(){
        for(int i = front; i != rear; i = (i+1) % array.length){
            System.out.println(array[i]);
        }
    }
}
